package com.funong.matrix.biz.user.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.funong.matrix.biz.base.dao.SysCompanyDao;
import com.funong.matrix.biz.base.entity.SysCompanyEntity;
import com.funong.matrix.biz.base.entity.SysDeptEntity;
import com.funong.matrix.biz.user.pojo.dto.CompanyDeptDTO;
import com.funong.matrix.biz.user.pojo.dto.SysCompanyDTO;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.dto.Result;
import com.funong.matrix.core.enums.IsDeletedEnum;
import com.funong.matrix.core.enums.StatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wanjialin
 * @version 1.0
 * @date 2023/5/18
 */
@Service
public class SysCompanyDomainDao {

    @Resource
    private SysCompanyDao sysCompanyDao;
    @Resource
    private SysDeptDomainDao sysDeptDomainDao;

    public List<CompanyDeptDTO> queryCompanyDept(SysCompanyEntity sysCompanyEntity) {

        List<CompanyDeptDTO> companyDeptDTOS = new ArrayList<>();

        List<SysCompanyEntity> sysCompanyEntities = sysCompanyDao.queryCompanyDept(sysCompanyEntity);

        for (SysCompanyEntity sysCompanyEntity1 : sysCompanyEntities) {
            CompanyDeptDTO companyDeptDTO = new CompanyDeptDTO();

            BeanUtils.copyProperties(sysCompanyEntity1, companyDeptDTO);
            //获取子公司信息
            List<SysCompanyEntity> sonCompanyList = sysCompanyDao.queryCompanyByParentId(sysCompanyEntity1.getId(), null);

            List<SysCompanyDTO> sysCompanyDTOS = new ArrayList<>();
            //遍历子公司
            for (SysCompanyEntity sonCompany : sonCompanyList) {
                SysCompanyDTO sysCompanyDTO = new SysCompanyDTO();
                BeanUtils.copyProperties(sonCompany, sysCompanyDTO);

                //获取部门信息
                List<SysDeptEntity> sysDeptEntities = sysDeptDomainDao.queryDeptByCompanyCode(sonCompany.getCode());

                sysCompanyDTO.setSysCompanyDTOS(sysDeptEntities);
                sysCompanyDTOS.add(sysCompanyDTO);
            }

            companyDeptDTO.setSysCompanyDTOS(sysCompanyDTOS);

            companyDeptDTOS.add(companyDeptDTO);
        }


        return companyDeptDTOS;
    }

    public List<CompanyDeptDTO> queryCompanyDendrogram(SysCompanyEntity sysCompanyEntity) {

        List<CompanyDeptDTO> companyDeptDTOS = new ArrayList<>();

        List<SysCompanyEntity> sysCompanyEntities = sysCompanyDao.queryCompanyDept(sysCompanyEntity);

        for (SysCompanyEntity sysCompanyEntity1 : sysCompanyEntities) {
            CompanyDeptDTO companyDeptDTO = new CompanyDeptDTO();

            BeanUtils.copyProperties(sysCompanyEntity1, companyDeptDTO);
            //获取子公司信息
            List<SysCompanyEntity> sonCompanyList = sysCompanyDao.queryCompanyByParentId(sysCompanyEntity1.getId(), null);

            List<SysCompanyDTO> sysCompanyDTOS = new ArrayList<>();
            //遍历子公司
            for (SysCompanyEntity sonCompany : sonCompanyList) {
                SysCompanyDTO sysCompanyDTO = new SysCompanyDTO();
                BeanUtils.copyProperties(sonCompany, sysCompanyDTO);
                sysCompanyDTOS.add(sysCompanyDTO);
            }

            companyDeptDTO.setSysCompanyDTOS(sysCompanyDTOS);

            companyDeptDTOS.add(companyDeptDTO);
        }

        return companyDeptDTOS;
    }


    public Result queryCompany(QueryDTO<SysCompanyEntity> queryDTO) {
        IPage<SysCompanyEntity> sysCompanyEntityIPage = sysCompanyDao.queryCompany(queryDTO);

        ObjectMapper mapper = new ObjectMapper();
        SysCompanyEntity sysCompanyEntity = mapper.convertValue(queryDTO.getCondition(), SysCompanyEntity.class);
        if (StringUtils.isEmpty(sysCompanyEntity.getName())) {
            return Result.page(sysCompanyEntityIPage, companyDeptParameter(sysCompanyEntityIPage, queryDTO.getCondition()));
        }

        return Result.page(sysCompanyEntityIPage);
    }


    public List<CompanyDeptDTO> companyDeptParameter(IPage<SysCompanyEntity> sysCompanyEntityIPage, SysCompanyEntity condition) {

        return sysCompanyEntityIPage.getRecords().stream().map(sysCompanyEntity -> {
                    CompanyDeptDTO companyDeptDTO = new CompanyDeptDTO();
                    BeanUtils.copyProperties(sysCompanyEntity, companyDeptDTO);
                    //获取子公司信息
                    List<SysCompanyEntity> sonCompanyList = sysCompanyDao.queryCompanyByParentId(sysCompanyEntity.getId(), condition);

                    List<SysCompanyDTO> sysCompanyDTOS = new ArrayList<>();
                    //遍历子公司
                    for (SysCompanyEntity sonCompany : sonCompanyList) {
                        SysCompanyDTO sysCompanyDTO = new SysCompanyDTO();
                        BeanUtils.copyProperties(sonCompany, sysCompanyDTO);
                        sysCompanyDTOS.add(sysCompanyDTO);
                    }

                    companyDeptDTO.setSysCompanyDTOS(sysCompanyDTOS);
                    return companyDeptDTO;
                }
        ).collect(Collectors.toList());
    }


    public SysCompanyEntity queryCompanyById(Integer Id) {
        return sysCompanyDao.getById(Id);
    }

    public SysCompanyEntity queryCompanyByCode(String companyCode) {

        List<SysCompanyEntity> sysCompanyEntities = sysCompanyDao.queryCompanyByCode(companyCode);
        return sysCompanyEntities.isEmpty() ? null : sysCompanyEntities.get(0);
    }

    public SysCompanyEntity queryCompanyAllByCode(String companyCode) {

        List<SysCompanyEntity> sysCompanyEntities = sysCompanyDao.queryCompanyAllByCode(companyCode);
        return sysCompanyEntities.isEmpty() ? null : sysCompanyEntities.get(0);
    }

    public Boolean saveCompany(SysCompanyEntity sysCompanyEntity) {

        SysCompanyEntity companyEntity = sysCompanyDao.queryCompanyDesc();
        String code;
        if (null == companyEntity) {
            code = generateCompanyCode(null);
        } else {
            code = generateCompanyCode(companyEntity.getCode());
        }

        if (null != sysCompanyEntity.getParentId() && sysCompanyEntity.getParentId() > 0) {
            SysCompanyEntity parentCompanyEntity = sysCompanyDao.getById(sysCompanyEntity.getParentId());
            sysCompanyEntity.setParentCode(parentCompanyEntity.getCode());
        }
        sysCompanyEntity.setCode(code);

        return sysCompanyDao.save(sysCompanyEntity);
    }


    public Boolean updateCompany(SysCompanyEntity sysCompanyEntity) {
        return sysCompanyDao.updateById(sysCompanyEntity);
    }


    public Boolean deleteCompany(Integer id) {
        SysCompanyEntity sysCompanyEntity = sysCompanyDao.getById(id);

        sysCompanyEntity.setIsDeleted(IsDeletedEnum.DELETED.getValue());

        return sysCompanyDao.updateById(sysCompanyEntity);
    }

    public Boolean updateCompanyStatus(Integer id) {

        SysCompanyEntity sysCompanyEntity = sysCompanyDao.getById(id);

        if (StatusEnum.ENABLE.getValue().equals(sysCompanyEntity.getStatus())) {

            sysCompanyEntity.setStatus(StatusEnum.DISABLE.getValue());
        } else {
            sysCompanyEntity.setStatus(StatusEnum.ENABLE.getValue());

        }

        return sysCompanyDao.updateById(sysCompanyEntity);
    }


    public String generateCompanyCode(String companyCode) {

        String code;

        if (StringUtils.isNotEmpty(companyCode)) {
            String no = companyCode.substring(1);

            Integer newCode = 1 + Integer.parseInt(no);

            code = String.format("C" + "%05d", newCode);
        } else {
            code = "C00001";
        }

        return code;
    }

}
